﻿using JobSearcherAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Web;

namespace JobSearcherAPI.Search
{
    internal class IndeedSearcher : XmlSearcher
    {
        const string BASE_URL = "http://api.indeed.com/ads/apisearch?";
        const string PUBLISHER_ID = "publisher=";
        const string VERSION = "&v=2";
        const string FORMAT = "&format=xml";
        const string QUERY = "&q=";
        const string LOCATION = "&l=";
        const string SORT = "&sort=";
        const string RADIUS = "&radius=";
        const string JOB_TYPE = "&jt=";
        const string FILTER = "&filter=";

        protected override string CreateSearchAddress(SearchArguments arguments)
        {
            stringBuilder
                .Clear()
                .Append(BASE_URL)
                .Append(PUBLISHER_ID).Append(API_ID)
                .Append(VERSION)
                .Append(FORMAT)
                .Append(QUERY).Append(arguments.Keywords.UrlEncoded())
                .Append(LOCATION).Append(arguments.Location.UrlEncoded())
                .Append(SORT).Append(arguments.Sort.UrlEncoded())
                .Append(RADIUS).Append(arguments.SearchRadius)
                .Append(JOB_TYPE).Append(arguments.JobType.UrlEncoded())
                .Append(FILTER).Append(arguments.Omitted.UrlEncoded());
            return stringBuilder.ToString();
        }

        protected override void SetUpAttributeStrings()
        {
            title = "jobtitle";
            location = "formattedLocation";
            description = "snippet";
        }

        protected override XmlNodeList GetJobNodes()
        {
            var responseNodes = doc.SelectSingleNode("response");
            var resultsNodes = responseNodes.SelectSingleNode("results");
            var returnNodes = resultsNodes.ChildNodes;
            return returnNodes;
        }
    }
}
